package com.bnz.shop.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.bnz.common.PageUtils;
import com.bnz.common.entity.R;
import com.bnz.shop.entity.ItemCatEntity;
import com.bnz.shop.entity.group.Goods;
import com.bnz.shop.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bnz.shop.entity.GoodsEntity;
import com.bnz.shop.service.GoodsService;



/**
 * 商品
 *
 * @author hbxfwf
 * @email 2568783935@qq.com
 * @date 2023-02-16 09:57:43
 */
@RestController
@RequestMapping("shop/goods")
public class GoodsController {
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private ItemCatService itemCatService;
	/**
	 * 列表
	 */
	@GetMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils pageUtils = goodsService.queryPage(params);

		return R.ok().put("page", pageUtils);
	}

	//根据父id查询对应下面的子节点
	@GetMapping("/findByParentId/{parentId}")
	public R findByParentId(@PathVariable String parentId){
		List<ItemCatEntity> itemCatEntities = itemCatService.findByParentId(parentId);

		return R.ok().put("categoryList",itemCatEntities);
	}
	/**
	 * 信息
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) {
		GoodsEntity goods = goodsService.getById(id);

		return R.ok().put("goods", goods);
	}
	// 根据商品id查询组合对象
	@GetMapping("/findById/{id}")
	public R findById(@PathVariable String id){
		Goods goods = goodsService.findById(id);
		return R.ok().put("goods",goods);
	}
	/**
	 * 保存
	 */
	@PostMapping("/save")
	public R save(@RequestBody Goods goods) {
		goodsService.insert(goods);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@PutMapping("/update")
	public R update(@RequestBody Goods goods) {
		goodsService.updateGoods(goods);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@DeleteMapping("/delete")
	public R delete(@RequestBody String[] ids) {
		goodsService.removeByIds(Arrays.asList(ids));

		return R.ok();
	}

}
